package com.xqh.controller.pms;

import com.xqh.commom.BaseController;
import com.xqh.base.mybatis.page.Page;
import com.xqh.base.util.response.RespMsg;
import com.xqh.base.util.serializer.ObjectUtils;
import com.xqh.entity.pms.SysRole;
import com.xqh.entity.pms.SysUser;
import com.xqh.service.pms.SysRoleService;
import com.xqh.service.pms.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by leo on 2017/11/6.
 */
@RequestMapping("/pms/user")
@Controller
public class SysUserController extends BaseController{

    @Autowired
    private SysUserService userService;

    @Autowired
    private SysRoleService roleService;

    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(){
        return new ModelAndView("pms/user/user_list");
    }

    @RequestMapping(value = "listData")
    public @ResponseBody Page<SysUser> listData(Page page, SysUser dto){
        userService.findByPage(page, ObjectUtils.changeToMap(dto));
        return page;
    }

    /**
     * 添加 编辑页面
     * @param model
     * @return
     */
    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public ModelAndView edit(ModelMap model){
        SysRole dto=new SysRole();
        dto.setExceptRoleType("SUPER_ADMIN");//过滤掉超级管理员
        model.put("roleList",roleService.findByBiz(ObjectUtils.changeToMap(dto)));
        if(StringUtils.hasText(getString("id"))){
            model.put("data",userService.findById(Long.valueOf(getString("id"))));
        }
        return new ModelAndView("pms/user/user_edit");
    }

    @RequestMapping(value = "saveData",method = RequestMethod.POST)
    public @ResponseBody RespMsg<?> saveData(SysUser dto){
        try {
            putOperatorInfo(dto);
            userService.saveData(dto);
        }catch (Exception e){
            return RespMsg.failResp(e.getMessage());
        }
        return RespMsg.successResp();
    }

    /**
     * 启用禁用
     * @param dto
     * @return
     */
    @RequestMapping(value = "modifyData",method = RequestMethod.POST)
    public @ResponseBody RespMsg<?> modifyData(SysUser dto){
         try {
             userService.modifyData(dto);
         }catch (Exception e){
             return RespMsg.failResp(e.getMessage());
         }
         return RespMsg.successResp();
    }
}
